package com.huidz.calendar.activityadapter;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.huidz.calendar.R;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.LunarCalendar;
import com.huidz.calendar.util.MyDates;
import com.huidz.calendar.util.TianGanDizhiShengXiao;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class YiJiAdapter extends BaseAdapter {
    ArrayList<String> list;
    Context context;
    Activity activity;

    public YiJiAdapter(ArrayList<String> list, Context context,Activity activity) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.yijilist, null);
        init i = new init();
        i.bwl_list_msg_time = convertView.findViewById(R.id.bwl_list_msg_time);
        i.yiji_date = convertView.findViewById(R.id.yiji_date);
        i.yiji_week = convertView.findViewById(R.id.yiji_week);
        i.yiji_almanac = convertView.findViewById(R.id.yiji_almanac);
        i.yiji_almanac_ymd = convertView.findViewById(R.id.yiji_almanac_ymd);
        i.yiji_almanac_shiershen = convertView.findViewById(R.id.yiji_almanac_shiershen);
        i.xingxiu = convertView.findViewById(R.id.xingxiu);
        i.julitianshu = convertView.findViewById(R.id.julitianshu);
        long getlog = 0;
        try {
            Apiutils.setlog("longfag="+list.get(position));
            getlog = Long.valueOf(dateToStamp(list.get(position)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        i.bwl_list_msg_time.setText(getDateToStrings(getlog));
        getDateToString(getlog);
        i.yiji_date.setText(day);
        i.yiji_week.setText(getWeek(getlog));
        i.yiji_almanac.setText("农历 "+s);
        i.yiji_almanac_ymd.setText(Apiutils.Alls+"「属" + TianGanDizhiShengXiao.getAnimalYearName(y) + "」");
        SelectCode(list.get(position));
        i.yiji_almanac_shiershen.setText("值神 "+LunarCalendar.GetZhishen(ms, ds));
        i.xingxiu.setText("星宿 "+ LunarCalendar.Getxingxiu(list.get(position)));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); //创建对象制定日期格式
        String date = format.format(new Date());
        i.julitianshu.setText(dateDiff(date,list.get(position))+"天后");
        convertView.setTag(i);
        return convertView;
    }


    public long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + hour + "小时" + min
                    + "分钟" + sec + "秒。");
            if (day>=1) {
                return day;
            }else {
                if (day==0) {
                    return 1;
                }else {
                    return 0;
                }

            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }
    public static String yuegan, rigan;
    public  SQLiteDatabase database;
    int ms=0,ds=0;
    public  void SelectCode(String _Date) {
        File file = new File(context.getFilesDir(), "saa.db");
        database = SQLiteDatabase.openDatabase(file.getAbsolutePath(), null, SQLiteDatabase.OPEN_READONLY);
        Cursor mCursor = database.query("IndexTable", null, "_Date=?", new String[]{_Date}, null,
                null, null);
        yuegan = Apiutils.Yuetgs.substring(1, 2);
        rigan = Apiutils.Ritgs.substring(1, 2);
        while (mCursor.moveToNext()) {
            for (int m = 0; m < LunarCalendar.Zhi.length; m++) {
                if (LunarCalendar.Zhi[m].equals(yuegan)) {
                    ms = m;
                    break;
                }
            }
            for (int d = 0; d < LunarCalendar.Zhi.length; d++) {
                if (LunarCalendar.Zhi[d].equals(rigan)) {
                    ds = d;
                    break;
                }
            }
        }
    }

    public static class init {
        TextView bwl_list_msg_time, yiji_date, yiji_week, yiji_almanac, yiji_almanac_ymd, yiji_almanac_shiershen, xingxiu, julitianshu;
    }

    public static String s;

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    String day = "";
    int y=0;

    public String getDateToString(long milSecond) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        y = Integer.parseInt(format.format(date).substring(0, 4));
        int m = Integer.parseInt(format.format(date).substring(5, 7));
        int r = Integer.parseInt(format.format(date).substring(8, 10));
        day = format.format(date).substring(8, 10);
        s = LunarCalendar.getLunarString(y, m, r);
        MyDates.initGanZhi(Integer.parseInt(format.format(date).substring(0, 4)), Integer.parseInt(format.format(date).substring(5, 7)), Integer.parseInt(format.format(date).substring(8, 10)));
        return format.format(date);
    }

    public String getDateToStrings(long milSecond) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM");
        return format.format(date);
    }

    public static String getWeek(long time) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time));
        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期
        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "周日";
                break;
            case Calendar.MONDAY:
                weekString = "周一";
                break;
            case Calendar.TUESDAY:
                weekString = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekString = "周三";
                break;
            case Calendar.THURSDAY:
                weekString = "周四";
                break;
            case Calendar.FRIDAY:
                weekString = "周五";
                break;
            default:
                weekString = "周六";
                break;

        }
        return weekString;
    }
}

